package com.lmonkey.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@WebFilter("/shangc/remimaupdate")
public class RemimaUpdate implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest reqq = (HttpServletRequest)req;
        HttpServletResponse respp = (HttpServletResponse) resp;
        //设置字符集
        reqq.setCharacterEncoding("UTF-8");
        respp.setContentType("text/html;charset=utf-8");
        PrintWriter out = respp.getWriter();

        //验证密码以及确认密码
        String password = reqq.getParameter("newmima");
        String pass = reqq.getParameter("again");
        if (password.equals("")||pass.equals("")){
            out.println("<script>alert('密码或确认密码不能为空');location.href='/lyq/shangc/forget.jsp';</script>");
            out.close();
            return;
        }else if (!password.equals(pass)){
            out.println("<script>alert('两次密码输入不一致');location.href='/lyq/shangc/forget.jsp';</script>");
            out.close();
            return;
        }else{
            Pattern pt = Pattern.compile("^[A-Za-z0-9-]{6,15}$");
            Matcher mt = pt.matcher(password);
            if (!mt.matches()){
                out.println("<script>alert('密码格式有误');location.href='/lyq/shangc/forget.jsp';</script>");
                out.close();
                return;
            }
        }
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
